package com.itheima.day10.myStream.test6;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> al = new ArrayList<>();
        addPlus(al, 20, 20);
        printList(al);
        // toList
        // al = removeOdd(al);
        // toSet
        al = removeOdd2(al);
        printList(al);


    }

    // 给集合添加元素
    public static void addPlus(List list, int number, int max) {
        Random r = new Random();
        for (int i = 0; i < number; i++) {
            list.add(r.nextInt(max) + 1);
        }
    }

    // 删除集合中的奇数 不去重
    public static ArrayList<Integer> removeOdd(ArrayList<Integer> list) {
        List<Integer> collect = list.stream().filter(new Predicate<Integer>() {
            @Override
            public boolean test(Integer i) {
                if (i % 2 == 0)
                    return true;
                return false;
            }
        }).collect(Collectors.toList());
        list.clear();
        for (Integer integer : collect) {
            list.add(integer);
        }
        return list;
    }

    // 删除集合中的奇数 去重
    public static ArrayList<Integer> removeOdd2(ArrayList<Integer> list){
        Set<Integer> collect = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toSet());
        list.clear();
        for (Integer integer : collect) {
            list.add(integer);
        }
        return list;
    }

    // 打印集合
    public static void printList(List list) {
        System.out.print("[");
        for (int i = 0; i < list.size() - 1; i++) {
            System.out.print(list.get(i) + ", ");
        }
        System.out.println(list.get(list.size() - 1) + "]");
    }
}
